//
//  ViewController.m
//  RocRemoteControlProject
//
//  Created by 李照鹏 on 2018/12/13.
//  Copyright © 2018年 Roc. All rights reserved.
//

#import "ViewController.h"
#import "RocProgrammingVC.h"
#import "RocRemoteControlVC.h"
#import "RocBluetoothVC.h"
#import "RocNavigationController.h"
#import "RocAlertView.h"

@interface ViewController ()

@property (nonatomic, strong) RocRemoteControlVC *controlVC;
@property (nonatomic, strong) RocProgrammingVC *programVC;

@property (nonatomic, strong) UIViewController *currentVC;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self basicSet];
    
    [self buildMianUI];
    
    [self addNotification];
    
    [self startBluetooth];
}

//支持旋转
-(BOOL)shouldAutorotate{
    return YES;
}
//支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscapeRight;
}
//一开始的方向  很重要
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationLandscapeRight;
}

#pragma mark - 基础设置
- (void)basicSet
{
    self.view.backgroundColor = [UIColor whiteColor];
}

#pragma mark - 搭建界面
- (void)buildMianUI
{
    [self buildProgrammingVC];
    [self buildControlVC];
}

- (void)buildProgrammingVC
{
    RocProgrammingVC *vc = [[RocProgrammingVC alloc] init];
    vc.view.frame = self.view.bounds;
    [self addChildViewController:vc];
    [self.view addSubview:vc.view];
    
    self.programVC = vc;
    self.currentVC = vc;
}
- (void)buildControlVC
{
    RocRemoteControlVC *vc = [[RocRemoteControlVC alloc] init];
    vc.view.frame = self.view.bounds;
    [self addChildViewController:vc];
    
    self.controlVC = vc;
}

#pragma Mark - 蓝牙相关
/**
  蓝牙逻辑
 1. 启动APP之后监控蓝牙
 2. 蓝牙启动，判断是否有本地设备数据。
 3. 蓝牙未启动，弹窗提示跳转设置界面
 4. 步骤2- 有本地数据直接连接 无本地数据弹窗提示跳转设置界面
 5.
 
 */
/**
 启动蓝牙
 */
- (void)startBluetooth
{
    __weak typeof(self) weakSelf = self;
    [[RocBluetoothManager Manager] startMonitorBluetoothStatus:^(BOOL isOpen, NSString * _Nonnull tips) {
        if (isOpen) {// 蓝牙可以使用
            [weakSelf haveOrNotDeviceInfo];
        }else{// 蓝牙不能使用
            [weakSelf alertForBluetooth:tips];
        }
        
    }];
}

/**
  蓝牙打开 判断是否有保存设备信息
 */
- (void)haveOrNotDeviceInfo
{
    [self alertForBluetooth:@"设备未连接"];
}

/**
  弹窗
 */
- (void)alertForBluetooth:(NSString *)tips
{
    __weak typeof(self) weakSelf = self;
    NSString *message = [NSString stringWithFormat:@"%@，请前往绑定设备界面设置！",tips];
    [RocAlertView ShowInView:self.view Tips:message CallBack:^(BOOL YesOrNo) {
        if (YesOrNo) {
            [weakSelf presentToBluetoothVC];
        }
    }];
    
}
#pragma mark - 通知以及通知处理
- (void)addNotification
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(show_RemoteControlVC) name:ROC_Notification_Name_SwitchToControl object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(show_ProgrammingVC) name:ROC_Notification_Name_SwitchToProgramming object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(presentToBluetoothVC) name:ROC_Notification_Name_SwitchToBluetoothVC object:nil];
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

// 展示编程界面
- (void)show_ProgrammingVC
{
    if (self.currentVC == self.programVC) {
        return;
    }
    
    [self transitionFromViewController:self.currentVC toViewController:self.programVC duration:0.2 options:UIViewAnimationOptionCurveEaseOut animations:^{
        
    } completion:^(BOOL finished) {
        self.currentVC = self.programVC;
    }];
}
// 展示遥控界面界面
- (void)show_RemoteControlVC
{
    if (self.currentVC == self.controlVC) {
        return;
    }
    
    [self transitionFromViewController:self.currentVC toViewController:self.controlVC duration:0.2 options:UIViewAnimationOptionCurveEaseOut animations:^{
        
    } completion:^(BOOL finished) {
        self.currentVC = self.controlVC;
    }];
}
// 跳转到蓝牙界面
- (void)presentToBluetoothVC
{
    RocBluetoothVC *vc = [[RocBluetoothVC alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    [self.currentVC presentViewController:nav animated:YES completion:^{
        
    }];
}


@end
